package com.shanghaionstar.notification.service;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import com.onstar.cn.common.exception.HttpException;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.network.http.ResponseHandler;
import com.onstar.cn.mag.publicService.DeviceInfoEntity;
import com.shanghaionstar.notification.bean.NotificationConstants;
import com.shanghaionstar.notification.service.impl.InterfaceServiceImpl;
import com.shanghaionstar.notification.util.ShareUtils;
import com.shanghaionstar.utils.BusinessUtil;

public class NotificationService extends Service {

	private InterfaceService service;
	private RequestParams params;
	private int retryTimes = 2;
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == NotificationConstants.SUCCESS) {
				service.connect(params);
			} else if (msg.what == NotificationConstants.FAIL) {
				if (params.isAutoRetry()) {
					if (retryTimes-- > 0) {
						saveDeviceInfo();
					}else {
						stopSelf();
					}
				}
			}
		}
	};

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		params = (RequestParams)intent.getSerializableExtra("params");
		if (params == null) {
			params = RequestParams.getDefaultParams(this);
		}
		//service = new InterfaceServiceImpl(this, NotificationConstants.URL_APIGEE);
		service = new InterfaceServiceImpl(this, Constants.CURRENT_SERVER_URL);
		saveDeviceInfo();
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		service.disconnect();
	}

	private void saveDeviceInfo() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				if(ShareUtils.getBoolean("saveDeviceFlag", NotificationService.this)) {
					handler.sendEmptyMessage(NotificationConstants.SUCCESS);
				}else {
					DeviceInfoEntity req = createSaveDeiveRequest();
					service.saveDeviceInfo(req, new ResponseHandler() {
						@Override
						public void onSuccess(String tag, String json) {
//							params.setDeviceToken(res.getMessage().getToken());
							ShareUtils.put("saveDeviceFlag", true, NotificationService.this);
							handler.sendEmptyMessage(NotificationConstants.SUCCESS);
						}
						
						@Override
						public void onError(String tag, HttpException e) {
							handler.sendEmptyMessage(NotificationConstants.FAIL);
						}
					});
//					if (res != null && res.getMessage() != null && NotificationConstants.MESSAGE_SUCCESS.equals(res.getMessage().getMessageCode())) {
//						params.setDeviceToken(res.getMessage().getToken());
//						ShareUtils.put("saveDeviceFlag", true, NotificationService.this);
//						handler.sendEmptyMessage(NotificationConstants.SUCCESS);
//					} else {
//						handler.sendEmptyMessage(NotificationConstants.FAIL);
////						handler.sendEmptyMessage(NotificationConstants.SUCCESS);
//					}
				}
			}
		}).start();
	}

	private DeviceInfoEntity createSaveDeiveRequest() {
		DeviceInfoEntity req = new DeviceInfoEntity();
		req.setDeviceID(params.getDeviceId());
		req.setDeviceOS(NotificationConstants.DEVICE_OS);
		req.setDeviceToken(params.getDeviceId());
		req.setDeviceType(NotificationConstants.DEVICETYPE);
		req.setIsAlert("Y");
		req.setIsNotification("Y");
		req.setIsBind("Y");
		return req;
	}

}
